#!/bin/bash
# 自动备份时使用以下命令
# /bin/bash  /sharedfolders/docker/dco_project/gitea/backup.sh

backup_path="/backup"
backup_filename=${backup_path}/$(date "+gitea-backup-%Y%m%d_%H%M%S")

# if sudo -n uptime 2>&1 | grep -q "load"; then
#   echo "请用root权限运行"
#   exit 1
# fi

cd $(dirname ${BASH_SOURCE:-$0})

cid=$(docker-compose ps -q gitea)
if [ ${#cid} -gt 0 ]; then
  docker-compose down
fi

# cd /
echo "备份到  ${backup_filename}.tgz ..."
docker-compose run --rm  gitea sh -c "/app/gitea/gitea -v >/data/gitea/version.txt &&cd /data &&sqlite3 /data/gitea/gitea.db .dump >/data/gitea/gitea.sql &&tar -zcpf ${backup_filename}.tgz ssh gitea git && chown git:users /backup -R && chmod -R a+r ${backup_path}/*"

echo "备份完毕.请查看 ${backup_filename}.tgz"

echo "启动 Gitea ..."
cd $(dirname ${BASH_SOURCE:-$0})
docker-compose pull
docker-compose up -d